package com.linkhub.oms.data.sync.api.yunexpress.domain.response;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.annotation.JSONField;
import com.linkhub.oms.common.util.QtOptionalUtil;
import com.linkhub.oms.common.util.QtStreamUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * @Description: 查询预估运费响应
 * @Author: echo
 * @Date: 2024/5/25 16:41
 * @Version: 1.0
 */
@Data
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
public class PriceTrialResponse extends YunExpressBaseResponse implements Serializable {


    @JSONField(serialize = false, deserialize = false)
    public boolean success() {
        return SUCCESS_CODE.equals(getCode()) && CollUtil.isNotEmpty(items);
    }

    public BigDecimal acquireTotalFee(String channelCode) {
        if (!success()) {
            return null;
        }

        List<Items> items = getItems();
        Items channel = QtStreamUtil.findFirst(items, item -> Objects.equals(item.getCode(), channelCode));
        return QtOptionalUtil.of(channel)
                .map(Items::getTotalFee)
                .orElse(null);
    }


    /**
     * items
     */
    @JSONField(name = "Items")
    private List<Items> items;
    /**
     * code
     */
    @JSONField(name = "Code")
    private String code;
    /**
     * message
     */
    @JSONField(name = "Message")
    private String message;
    /**
     * requestId
     */
    @JSONField(name = "RequestId")
    private String requestId;
    /**
     * timeStamp
     */
    @JSONField(name = "TimeStamp")
    private String timeStamp;

    /**
     * Items
     */
    @NoArgsConstructor
    @Data
    public static class Items {
        /**
         * code
         */
        @JSONField(name = "Code")
        private String code;
        /**
         * cName
         */
        @JSONField(name = "CName")
        private String cName;
        /**
         * eName
         */
        @JSONField(name = "EName")
        private String eName;
        /**
         * shippingFee
         */
        @JSONField(name = "ShippingFee")
        private BigDecimal shippingFee;
        /**
         * registrationFee
         */
        @JSONField(name = "RegistrationFee")
        private BigDecimal registrationFee;
        /**
         * fuelFee
         */
        @JSONField(name = "FuelFee")
        private BigDecimal fuelFee;
        /**
         * sundryFee
         */
        @JSONField(name = "SundryFee")
        private BigDecimal sundryFee;
        /**
         * tariffPrepayFee
         */
        @JSONField(name = "TariffPrepayFee")
        private Object tariffPrepayFee;
        /**
         * totalFee
         */
        @JSONField(name = "TotalFee")
        private BigDecimal totalFee;
        /**
         * deliveryDays
         */
        @JSONField(name = "DeliveryDays")
        private String deliveryDays;
        /**
         * remark
         */
        @JSONField(name = "Remark")
        private String remark;
        /**
         * track
         */
        @JSONField(name = "Track")
        private String track;
        /**
         * goodsType
         */
        @JSONField(name = "GoodsType")
        private String goodsType;
        /**
         * weight
         */
        @JSONField(name = "Weight")
        private BigDecimal weight;
        /**
         * productGroupname
         */
        @JSONField(name = "product_groupname")
        private String productGroupname;
        /**
         * currency
         */
        @JSONField(name = "Currency")
        private String currency;
        /**
         * insuredFee
         */
        @JSONField(name = "InsuredFee")
        private BigDecimal insuredFee;
    }
}
